package com.kuangxiaoyang.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {
    private static final String TAG = "SplashActivity";
    Timer timer;
    TimerTask timerTask;
    int timeout=5;
    TextView mTvTimeout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initView(); // 绑定控件
        timer = new Timer(); // 创建定时器
        timerTask = new TimerTask() {
            @Override
            public void run() {
                timeout = timeout -1;
                Log.e(TAG, "run: "+timeout );
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mTvTimeout.setText(timeout+"S");
                    }
                });
                if(timeout <=0){
                    timerTask.cancel();
                    timer.cancel();
                    // 实现界面切换
                    Intent intent = new Intent(SplashActivity.this,
                            LoginActivity.class);
                    startActivity(intent);
                }
            }
        };
        timer.schedule(timerTask,1000,1000);


    }

    private void initView() {
        // 绑定控件
        mTvTimeout  = findViewById(R.id.tv_timeout);
    }
}